package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.*;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private SetmealDaoDetail setmealDaoDetail;
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckItemDao checkItemDao;


    /**
     * 新增套餐
     *
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {

        setmealDao.add(setmeal);
        Integer setmealId = setmeal.getId();
        for (Integer checkgroupId : checkgroupIds) {
            setmealDaoDetail.add(setmealId, checkgroupId);

        }
    }

    /**
     * 套餐分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Setmeal> page = setmealDao.pageQuery(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<Setmeal> seletAll() {
        List<Setmeal> setmealList = setmealDao.selectAll();
        return setmealList;
    }

    /**
     * 根据id套餐详情
     *
     * @param id
     */
    @Override
    public Setmeal findById(Integer id) {


        Setmeal setmeal = setmealDao.findById(id);
        List<CheckGroup> checkGroupList = checkGroupDao.findCheckGroupIdById(id);

        setmeal.setCheckGroups(checkGroupList);
        for (CheckGroup checkGroup : checkGroupList) {
            List<CheckItem> checkItems = checkItemDao.findCheckItemById(checkGroup.getId());
            checkGroup.setCheckItems(checkItems);
        }
        return setmeal;

    }
}
